﻿using System;
using System.Device.Location;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Reactive;
using System.Collections.Generic;

namespace Skyhook.WindowsPhone.AtoB {
    public partial class MainPage : PhoneApplicationPage {
        private readonly Random random = new Random();
        private readonly GeoCoordinateWatcher geoCoordinateWatcher = new GeoCoordinateWatcher();

        public static List<DataPoint> PointsLog {
            get {
                var app = Application.Current;
                return ((App)app).PointsLog;
            }
        }

        public MainPage() {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        private void SaveCoordinates(double latitude, double longitude) {
            if (PointsLog.Count == 0) {
                lblLatitudeA.Text = latitude.ToString();
                lblLongitudeA.Text = longitude.ToString();
            }

            PointsLog.Add(new DataPoint {
                Latitude = latitude,
                Longitude = longitude,
                DateCaptured = DateTime.Now
            });
        }
        private void cmdStart_Click(object sender, RoutedEventArgs e) {
            geoCoordinateWatcher.Start();
        }
        private void cmdEnd_Click(object sender, RoutedEventArgs e) {

        }

        private IObservable<GeoCoordinate> CreateObservableGeoPositionWatcher() {
            var observable = Observable.FromEvent<GeoPositionChangedEventArgs<GeoCoordinate>>(e => geoCoordinateWatcher.PositionChanged += e, e => geoCoordinateWatcher.PositionChanged -= e).Select(e => e.EventArgs.Position.Location);
            geoCoordinateWatcher.Start();
            return observable;
        }
        private IObservable<GeoCoordinate> CreateGeoPositionEmulator() {
            return Observable.Timer(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(1)).Select(l => CreateRandomCoordinate());
        }

        private GeoCoordinate CreateRandomCoordinate() {
            var latitude = (random.NextDouble() * 180.0) - 90.0;
            var longitude = (random.NextDouble() * 360.0) - 180.0;

            return new GeoCoordinate(latitude, longitude);
        }
        protected void OnLoaded(object sender, RoutedEventArgs e) {
            const bool useEmulation = true;
            var observable = useEmulation ? CreateGeoPositionEmulator() : CreateObservableGeoPositionWatcher();

            observable.ObserveOnDispatcher().Subscribe(OnPositionChanged);
        }
        private void OnPositionChanged(GeoCoordinate location) {
            lblLatitudeCurrent.Text = location.Latitude.ToString();
            lblLongitudeCurrent.Text = location.Longitude.ToString();
            SaveCoordinates(location.Latitude, location.Longitude);
        }

        private void cmdDetails_Click(object sender, RoutedEventArgs e) {
            var app = Application.Current;
            ((App)app).RootFrame.Navigate(new Uri("/Skyhook.WindowsPhone.AtoB;component/LoggedPoints.xaml", UriKind.Relative));
        }

        private void cmdViewMap_Click(object sender, RoutedEventArgs e) {
            var app = Application.Current;
            ((App)app).RootFrame.Navigate(new Uri("/Skyhook.WindowsPhone.AtoB;component/MapRoute.xaml", UriKind.Relative));
        }
    }
}